package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.ShopCommentAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.ShopCommentEntity;
import com.eightbears.beboy.entity.status.Status_Error;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.PARAMS_PAGE_SIZE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHOPS_DETAILS;
import static com.eightbears.beboy.base.util.CommonAPI.delayMillis;
import static com.eightbears.beboy.ui.user.activity.UserInfoActivity.PARAMS_USER_INFO;
import static com.eightbears.beboy.ui.user.fragment.FindFragment.PARAMS_SHOP_ID;

public class ShopCommentListActivity extends BaseActivity implements
        BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private int page = 1;
    private boolean isErr = true;
    private int isEnd = 0;
    private String shopId;

    private ShopCommentAdapter shopCommentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_comment_list);

        initView();
        initData();
        initEvent();

    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText(R.string.text_comment_list);
        shopId = getIntent().getStringExtra(PARAMS_SHOP_ID);
        initAdapter();
    }

    @Override
    protected void initData() {
        getShopComment();
    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });

        shopCommentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ShopCommentEntity.ShopComment shopComment =
                        (ShopCommentEntity.ShopComment) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.iv_user_head:
                        startLocalActivity(ShopCommentListActivity.this, UserInfoActivity.class,
                                PARAMS_USER_INFO, shopComment.getUser_id());
                        break;
                }
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    private void initAdapter() {
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color));
        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        shopCommentAdapter = new ShopCommentAdapter();
        shopCommentAdapter.openLoadAnimation();
        shopCommentAdapter.setOnLoadMoreListener(this);
        rv_list.setAdapter(shopCommentAdapter);
    }

    private void getShopComment() {
        String url = URL_SHOPS_DETAILS + shopId + "/comments";
        OkHttpUtils.get().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("page", String.valueOf(page))
                .addParams("size", PARAMS_PAGE_SIZE)
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                swipeLayout.setRefreshing(false);
                ShopCommentEntity shopCommentEntity = GsonUtil.fromJson(response, ShopCommentEntity.class);
                if (shopCommentEntity.getStatus_code().equals(CONNECTION_STATUS_SUCCESS)) {
                    shopCommentAdapter.setNewData(shopCommentEntity.getData().getList());
                    isEnd = shopCommentEntity.getData().getIs_end();
                    if (page == 1) {
                        shopCommentAdapter.setNewData(shopCommentEntity.getData().getList());
                    } else {
                        shopCommentAdapter.addData(shopCommentEntity.getData().getList());
                        shopCommentAdapter.loadMoreComplete();
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                swipeLayout.setRefreshing(false);
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(ShopCommentListActivity.this, status_error);
            }

        });
    }


    @Override
    public void onRefresh() {
        shopCommentAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isErr = true;
                page = 1;
                shopCommentAdapter.getData().clear();
                getShopComment();
                shopCommentAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isEnd == 1) {
                    shopCommentAdapter.loadMoreEnd(true);
                } else {
                    if (isErr) {
                        page++;
                        getShopComment();
                    } else {
                        isErr = true;
                        ShowToast.showShortToast(R.string.network_anomalies);
                        shopCommentAdapter.loadMoreFail();
                    }
                }
            }
        }, delayMillis);
    }
}
